/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.service.dao.impl;

import com.wisnu.spms.service.dao.DbBillingDao;
import com.wisnu.spms.service.entity.DbBilling;
import com.wisnu.spms.service.entity.DbStatus;
import com.wisnu.spms.service.entity.DbStudent;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class DbBillingDaoImpl extends HibernateDaoSupport implements DbBillingDao {

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void insertBilling(DbBilling billing) throws Exception {
        try {
            getHibernateTemplate().save(billing);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void updateBilling(DbBilling billing) throws Exception {
         try {
            getHibernateTemplate().update(billing);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<DbBilling> getBillings() throws Exception {
        try {
            return getHibernateTemplate().loadAll(DbBilling.class);
        } catch (DataAccessException t) {
            throw new Exception(t.getMessage());
        }
    }

    @Override
    public List<DbBilling> getDbBillingById(final DbStudent dbStudent) throws Exception {
        try {
            Object o = getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.createQuery("select a from DbBilling a where a.dbStudent = :id AND a.dbStatus = :status")
                            .setParameter("id", dbStudent)
                            .setParameter("status", new DbStatus(1, "Belum Lunas"))
                            .list();
                }
            });
            if (o == null) {
                throw new Exception("Student with ID " + dbStudent + " do not found");
            } else {
                return (List<DbBilling>) o;
            }
        } catch (Exception t) {
            throw new Exception(t.getMessage());
        }

    }

    @Override
    public List<DbBilling> GetDbBillingBySemester(final DbStudent dbStudent,final Integer semester) throws Exception {
        try {
            Object o = getHibernateTemplate().execute(new HibernateCallback() {

                @Override
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    return session.createQuery("select a from DbBilling a where a.dbStudent = :id AND a.studentSemester = :semester")
                            .setParameter("id", dbStudent)
                            .setParameter("semester", semester)
                            .list();
                }
            });
            if (o == null) {
                throw new Exception("Student with ID " + dbStudent + " do not found");
            } else {
                return (List<DbBilling>) o;
            }
        } catch (Exception t) {
            throw new Exception(t.getMessage());
        }
    }
    
    
    
}
